package aInterview.xiaomi;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
public class JiaoTi2 {
     ReentrantLock lock= new ReentrantLock();
     Condition conditionA=lock.newCondition();
     Condition conditionB=lock.newCondition();
     Condition conditionC=lock.newCondition();
     volatile  String  CODE="A";
     volatile  int count=1;

     public  void   printA(){
        lock.lock();
        try {
            while (!CODE.equals("A")){
                //TODO:2023/10/17 根据代码的提示把try  catch 写在了这个位置
//            try {
                conditionA.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            }
            System.out.println(Thread.currentThread().getName()+count);
            count++;
            CODE="B";
            conditionB.signalAll();
        }catch (InterruptedException  e){
            //TODO:2023/10/17 容易犯的错误
//            System.out.println(e.printStackTrace());
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public  void   printB(){
        lock.lock();
        try {
            while (!CODE.equals("B")){
                //TODO:2023/10/17 根据代码的提示把try  catch 写在了这个位置
//            try {
                conditionB.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            }
            System.out.println(Thread.currentThread().getName()+count);
            count++;
            CODE="C";
            conditionC.signalAll();
        }catch (InterruptedException  e){
            //TODO:2023/10/17 容易犯的错误
//            System.out.println(e.printStackTrace());
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public  void   printC(){
        lock.lock();
        try {
            while (!CODE.equals("C")){
                //TODO:2023/10/17 根据代码的提示把try  catch 写在了这个位置
//            try {
                conditionC.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            }
            System.out.println(Thread.currentThread().getName()+count);
            count++;
            CODE="A";
            conditionA.signalAll();
        }catch (InterruptedException  e){
            //TODO:2023/10/17 容易犯的错误
//            System.out.println(e.printStackTrace());
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        JiaoTi2 jiaoTi2 = new JiaoTi2();
        new Thread(()->{
            for (int i = 0; i < 34; i++) {
                jiaoTi2.printA();
            }
        },"线程A").start();
        new Thread(()->{
            for (int i = 0; i < 33; i++) {
                jiaoTi2.printB();
            }
        },"线程B").start();
        new Thread(()->{
            for (int i = 0; i < 33; i++) {
                jiaoTi2.printC();
            }
        },"线程C").start();

    }




}
